package com.xinyi.tangmi;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ViewSwitcher;

public class FlowActivity extends Activity {
	protected static final int GET_WEB_CMP = 0;

	protected static final int GET_WEB_FAIL = -1;

	String order_id = "10";
	Dialog dialog;
	String webInfo = "";
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
			switch (msg.what) {

			case GET_WEB_CMP:
				webView.getSettings().setDisplayZoomControls(true);
				webView.getSettings().setJavaScriptEnabled(true);
				webView.getSettings().setDefaultTextEncodingName("utf-8");
				webView.loadDataWithBaseURL(MainActivity.BASE_PATH, webInfo,
						"text/html", "utf-8", null);
				break;
			case GET_WEB_FAIL:

				break;

			default:
				break;
			}
		};
	};
	WebView webView;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		webView.destroy();
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_flow);
		order_id = getIntent().getStringExtra("order_id");
		getShippinginfo(order_id);
		webView = (WebView) findViewById(R.id.webview);
		findViewById(R.id.back_button).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
	}

	public void getShippinginfo(String id) {
		dialog = LoginActivity.createLoadingDialog(this, R.string.loading);
		dialog.show();
		final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("act", "shipping_info"));
		nameValuePairs.add(new BasicNameValuePair("order_id", order_id));
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					PostData postData = new PostData();
					webInfo = postData.getStringObject(
							OrderActivity.ORDER_PATH, new UrlEncodedFormEntity(
									nameValuePairs, HTTP.UTF_8));
					Log.i("webInfo", webInfo);
					if (webInfo != null && !webInfo.equals("")) {
						handler.sendEmptyMessage(GET_WEB_CMP);
					} else {
						handler.sendEmptyMessage(GET_WEB_FAIL);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					handler.sendEmptyMessage(GET_WEB_FAIL);
				}
			}
		}).start();
	}@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		MainActivity.sendNotification(this);
		super.onPause();
	}

}
